import axios from 'axios'


//  获取图片Blob Url
export const getImgBlogUrl = (url,authorization) => {
    return new Promise((resolve, reject) => {
        axios({
            method: 'get',
            responseType: 'blob',
            headers: {
                'Authorization': authorization
            },
            url: url
        }).then(res => {
            // console.log(res, 'res=======');
            if (res && res.data && res.data.size) {
                const dataInfo = res.data
                let reader = new window.FileReader()
                // 使用readAsArrayBuffer读取文件, result属性中将包含一个 ArrayBuffer 对象以表示所读取文件的数据
                reader.readAsArrayBuffer(dataInfo)
                reader.onload = function (e) {
                    const result = e.target.result
                    const contentType = dataInfo.type
                    // 生成blob图片,需要参数(字节数组, 文件类型)
                    const blob = new Blob([result], {
                        type: contentType
                    })
                    const url = window.URL.createObjectURL(blob)
                    console.log(url) // 产生一个类似 blob:d3958f5c-0777-0845-9dcf-2cb28783acaf 这样的URL字符串

                    resolve(url)
                }
            }
        }).catch(e => {
            reject(e)
        })
    })
}
